<template>
    <div class="cpt-page">
        <div class="cpt-page-header cpt-page-header-flex">
            <div class="flex-left">
                <el-button type="primary" @click="doSub">确认</el-button>
            </div>
            <div class="flex-right">
                <el-input v-model="query.name" class="search-input-class border" placeholder="办事处名称" />
                <el-select v-model="query.division" class="search-input-class border" placeholder="区域事业部">
                    <el-option
                        v-for="item in divisionOption"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    ></el-option>
                </el-select>
                <el-button type="primary" @click="loadData">查询</el-button>
                <el-button @click="reset">重置</el-button>
            </div>
        </div>
        <div class="cpt-content">
            <el-table
                ref="datagrid"
                :data="table.data"
                border
                height="350"
                @row-click="handleClickTableRow"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" />
                <el-table-column prop="name" label="办事处名称" min-width="200" show-overflow-tooltip />
                <el-table-column prop="customerManager" label="客户经理" min-width="120" show-overflow-tooltip />
                <el-table-column prop="division" label="事业部" min-width="200" show-overflow-tooltip />
                <el-table-column prop="marketingDirector" label="市场总监" min-width="120" show-overflow-tooltip />
            </el-table>
        </div>
        <div class="cpt-footer">
            <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="table.pagination.currentPage"
                :page-sizes="table.pagination.pageSizes"
                :page-size="table.pagination.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="table.pagination.total"
            ></el-pagination>
        </div>
    </div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElNotification } from 'element-plus'
import http from '@/http.js'

const props = defineProps({
    single: {
        type: Boolean,
        default: true
    }
})

const emit = defineEmits(['select'])

const datagrid = ref(null)

const query = reactive({
    name: '',
    division: ''
})

const table = reactive({
    data: [],
    selected: [],
    pagination: {
        currentPage: 1,
        pageSizes: [10, 20, 50, 100],
        pageSize: 10,
        total: 0
    }
})

const handleSelectionChange = (val) => {
    table.selected = val
}

const handleSizeChange = (val) => {
    table.pagination.pageSize = val
    loadData()
}

const handleCurrentChange = (val) => {
    table.pagination.currentPage = val
    loadData()
}

const handleClickTableRow = (row) => {
    if (props.single) {
        datagrid.value.clearSelection()
    }
    datagrid.value.toggleRowSelection(row)
}

const divisionOption= ref([])

const loadOption = () => {
    http.fetchForm({
        url: '/sys/dictionary/queryByGroups',
        method: 'get',
        data: {
            groupCodes: 'P-002-003'
        }
    }).then((res) => {
        divisionOption.value = res.data['P-002-003']
    })
}

const loadData = () => {
    let params = {
        name: query.name,
        division: query.division,
        page: table.pagination.currentPage,
        pageSize: table.pagination.pageSize
    }
    http.fetchForm({
        url: '/pub/office/grid',
        data: params
    }).then((res) => {
        table.data = res.data.grid
        table.pagination.total = res.data.total
    })
}

const reset = () => {
    query.name = ''
    query.division = ''
    loadData()
}

const doSub = () => {
    let selected = table.selected
    if (selected.length == 0) {
        ElNotification({
            title: '提示',
            message: '请选择',
            type: 'info'
        })
        return
    }
    if (props.single) {
        emit('select', selected[0])
    } else {
        emit('select', selected)
    }
}

// mounted
onMounted(() => {
    loadOption()
    loadData()
})
</script>
<style scoped>
@import '@/assets/css/list-cpt.css';
</style>
